package br.com.orion.sga.utils.validators;


import br.com.orion.sga.utils.validators.interfaces.Pessoa;

public class PessoaFisica extends Pessoa{

	@Override
	public boolean validDocument(String document) {
		//remove the all characters different of 0-9
		document = document.replace(".","").replace("-", "");
		
		int totalOfMultiply = 0;
		int digitvalidated = 0;
		int countBigToLess = 10;
		int restOfDivision = 0;
		
		/**
		 * Digit One
		 */
		int numericValue = 0;
		//calculate the digit one
		for (int i = 0; i < (document.length()-2); i++) {
			numericValue = Character.getNumericValue(document.charAt(i));
			totalOfMultiply += numericValue * (countBigToLess -i);
		}
		restOfDivision = totalOfMultiply % 11;
		if (restOfDivision > 2) {
			digitvalidated = 11 - restOfDivision;
		}
		//Check if digitValidated is equal to digit informed by User. If it is not so, return false
		if (digitvalidated != Character.getNumericValue(document.charAt(9))) {
			return false;
		}
		
		
		/**
		 * Digit Two
		 */
		numericValue = 0;
		totalOfMultiply = 0;
		countBigToLess++;//because the digit one is included
		//calculate the digit two
		for (int i = 0; i < (document.length()-1); i++) {
			numericValue = Character.getNumericValue(document.charAt(i));
			totalOfMultiply += numericValue * (countBigToLess -i);
		}
		restOfDivision = totalOfMultiply % 11;
		if (restOfDivision > 2) {
			digitvalidated = 11 - restOfDivision;
		}
		//Check if digitValidated is equal to digit informed by User. If it is not so, return false
		if (digitvalidated != Character.getNumericValue(document.charAt(10))) {
			return false;
		}
		
		return true;
		
	}
}
